//插入排序

#include <stdio.h>

void insertion_sort(int *arr, int size);
void show(int *arr, int len);

int main()
{
    int a[7] = {1, 2, 4, 5, 3, 9, 8};
    //数组长度
    int num = sizeof(a) / sizeof(a[0]);

    insertion_sort(a, num);

    show(a, num);

    return 0;
}

void insertion_sort(int *arr, int size)
{
    int i, j, tmp;
    for (i = 1; i < size; i++)
    {
        if (arr[i] < arr[i - 1])
        {
            tmp = arr[i];
            for (j = i - 1; j >= 0 && arr[j] > tmp; j--)
            {
                arr[j + 1] = arr[j];
            }
            arr[j + 1] = tmp;
        }
    }
}

void show(int *arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}